#include "stdafx.h"

#include "AccountTbl.h"
	

CAccountTbl::CAccountTbl()
{
}
bool CAccountTbl::checkAcc(CString sUser,CString sPass,CString& sCode)
{
	bool bReturn = false;
	CString sNewQuery;
	sNewQuery.Format(_T("SELECT * FROM %s.%s WHERE %s = '%s' AND %s = MD5('%s')"),g_sDatabase,TABLE_NAME_TK,FIELD_USER,sUser,FIELD_PASS,sPass);
	CT2A ascii(sNewQuery, CP_UTF8);	
	mysql_query(conn,ascii.m_psz);
	MYSQL_RES * pResult = mysql_use_result(conn);
	if (pResult)
	{
		MYSQL_ROW row = mysql_fetch_row(pResult);
		if (row)
		{
			sCode = row[2];
			bReturn = true;
		}
		mysql_free_result(pResult);
	}
	return bReturn;
}

bool CAccountTbl::isExitAcount(const CString& sUser)
{
	bool bReturn = false;
	CString sNewQuery;
	sNewQuery.Format(_T("SELECT * FROM %s.%s WHERE %s = '%s' "),g_sDatabase,TABLE_NAME_TK,FIELD_USER,sUser);
	CT2A ascii(sNewQuery, CP_UTF8);	
	mysql_query(conn,ascii.m_psz);
	MYSQL_RES * pResult = mysql_use_result(conn);
	if (pResult)
	{
		MYSQL_ROW row = mysql_fetch_row(pResult);
		if (row)
		{			
			bReturn = true;
		}
		mysql_free_result(pResult);
	}
	return bReturn;
}
bool CAccountTbl::insertAcount(CString sUser,CString sPass, CString sCodeStaff)
{
	CString sValue;

	sValue.Format(_T("( '%s',MD5('%s'), '%s', %d)"),sUser,sPass,sCodeStaff,0);		
	return insertRecord(TBL_ACCOUNT,sValue);
	
}
bool CAccountTbl::deleteAcount(CString sUser)
{
	CString sValue;
	bool bRes = false;
	sValue.Format(_T("DELETE FROM %s.%s WHERE %s = '%s'"),g_sDatabase,getTableAt(TBL_ACCOUNT),FIELD_USER,sUser);		
	if (conn)
	{
		CT2A ascii(sValue, CP_UTF8);
		if(mysql_query(conn,ascii.m_psz) == 0)
			bRes = true;		
	}
	return bRes;
}
bool CAccountTbl::updateAcount(CString sUser,CString sPass)
{
	CString sValue;
	bool bRes = false;
	sValue.Format(_T("UPDATE %s.%s SET %s = MD5('%s') WHERE %s = '%s'"),g_sDatabase,getTableAt(TBL_ACCOUNT),FIELD_PASS,sPass,FIELD_USER,sUser);		
	if (conn)
	{
		CT2A ascii(sValue, CP_UTF8);
		if(mysql_query(conn,ascii.m_psz) == 0)
			bRes = true;		
	}
	return bRes;
}
CString CAccountTbl::getUseFromAcount(CString sUser)
{	
	CString sCodeAcc = _T("");
	CString sNewQuery;
	sNewQuery.Format(_T("SELECT * FROM %s.%s WHERE %s = '%s'"),g_sDatabase,TABLE_NAME_TK,FIELD_USER,sUser);
	CT2A ascii(sNewQuery, CP_UTF8);	
	mysql_query(conn,ascii.m_psz);
	MYSQL_RES * pResult = mysql_use_result(conn);
	if (pResult)
	{
		MYSQL_ROW row = mysql_fetch_row(pResult);
		if (row)
		{
			sCodeAcc = CString(row[2]);
			
		}
		mysql_free_result(pResult);
	}
	return sCodeAcc;
}
vector<CString> CAccountTbl::getListAcc(CString sMSNV)
{
	vector<CString> vtAcc;

	MYSQL_RES * pResult = NULL;
	CString sNewQuery;
	mysql_query(conn,"SET character_set_results=utf8");	
	sNewQuery.Format(_T("SELECT * FROM %s.%s WHERE  %s = '%s'"),g_sDatabase,TABLE_NAME_TK,FIELD_CODE_STAFF,sMSNV);
	CT2A ascii(sNewQuery, CP_UTF8);		
	mysql_query(conn,ascii.m_psz);
	pResult = mysql_use_result(conn);
	if (pResult)
	{
		MYSQL_ROW row = NULL;
		while((row = mysql_fetch_row(pResult)) )
		{
			CString sTem = CString(row[0]);
			vtAcc.push_back(sTem);
		}
		mysql_free_result(pResult);
	}
	return vtAcc;
}

bool CAccountTbl::isEmpty()
{
	bool bReturn = false;
	CString sNewQuery;
	sNewQuery.Format(_T("SELECT COUNT(%s) FROM %s.%s"),FIELD_USER,g_sDatabase,TABLE_NAME_TK);
	CT2A ascii(sNewQuery, CP_UTF8);	
	mysql_query(conn,ascii.m_psz);
	MYSQL_RES * pResult = mysql_use_result(conn);
	if (pResult )
	{
		MYSQL_ROW row = mysql_fetch_row(pResult);
		if (row && atoi(row[0]) == 0)
			bReturn = true;
		mysql_free_result(pResult);
	}
	return bReturn;
}